package com.zhj.pay;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;

import java.io.File;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

public class PluginManager {
    private static final PluginManager sInstance = new PluginManager();


    private Resources resources;
    private DexClassLoader dexClassLoader;

    private Context context;
    private PackageInfo packageInfo;

    public PackageInfo getPackageInfo() {
        return packageInfo;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public Resources getResources() {
        return resources;
    }

    public DexClassLoader getDexClassLoader() {
        return dexClassLoader;
    }

    private PluginManager() {

    }


    public static PluginManager getsInstance() {
        return sInstance;
    }

    public void loadApk(String path) {
        File dexOutFile = context.getDir("dex", Context.MODE_PRIVATE); //dex优化的路径
        dexClassLoader = new DexClassLoader(path, dexOutFile.getAbsolutePath(), null, context.getClassLoader());

        packageInfo = context.getPackageManager().getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);


        //第一个参数最重要
        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetpath = AssetManager.class.getMethod("addAssetPath",String.class);
            addAssetpath.invoke(assetManager, path);
            resources = new Resources(assetManager, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
